﻿using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;

namespace ConfExpert.Forms.Rapor.Kartela
{
    public partial class KumasKartela : DevReport
    {
        public KumasKartela()
        {
            InitializeComponent();            
        }

        public KumasKartela(DataTable Kartela,int Sablon,string UstYazi,float Yukseklik):this()
        {
            Kartela.TableName = "Kartela";

            Detail.HeightF = tbKartela.HeightF = Yukseklik;
            if (Sablon == 3)
            {
                tbKartela.LocationFloat = new DevExpress.Utils.PointFloat(1450F, 0F);
                tcKumasSag.Dispose(); xrTableCell1.Dispose(); xrTableCell2.Dispose(); xrTableCell4.Dispose();
                tcKumasSol.WidthF = 250;
            }
            else if (Sablon == 4)
            {
                tbKartela.LocationFloat = new DevExpress.Utils.PointFloat(1450F, 0F);
                tcKumasSag.Dispose(); xrTableCell1.Dispose(); xrTableCell2.Dispose(); xrTableCell4.Dispose();
                tcKumasSol.WidthF = 250;
            }
            else if (Sablon == 5)
            {
                tbKartela.LocationFloat = new DevExpress.Utils.PointFloat(1300F, 0F);
                tcKumasSag.Dispose(); xrTableCell1.Dispose(); xrTableCell2.Dispose(); xrTableCell4.Dispose();
                tcKumasSol.WidthF = 250;
            }
            else if (Sablon == 6)
            {
                tbKartela.LocationFloat = new DevExpress.Utils.PointFloat(1300F, 0F);
                tcKumasSag.Dispose(); xrTableCell1.Dispose(); xrTableCell2.Dispose(); xrTableCell4.Dispose();
                tcKumasSol.WidthF = 250;
            }

            //DataSet ds = new DataSet();
            //ds.Tables.Add(Kartela);

            this.DataMember = "Kartela";
            this.DataSource = Kartela.DataSet;

            tcUstYazi.Text = UstYazi;

            tcKumasSol.DataBindings.Add("Text", null, "Kartela.KumasSol");
            if (Sablon == 1 || Sablon == 2)
                tcKumasSag.DataBindings.Add("Text", null, "Kartela.KumasSag");

            tcKumasSag.BeforePrint += cell_BeforePrint;
            tcKumasSol.BeforePrint += cell_BeforePrint;
        }

        void cell_BeforePrint(object sender, PrintEventArgs e)
        {
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            var labelRows = label.Text.Split(Environment.NewLine.ToCharArray());
            var longest = labelRows.OrderByDescending(s => s.Length).First();
            
            float newSize = 7f;
            label.Font = new Font("Calibri", newSize);

            float stringWidth = gr.MeasureString(longest, label.Font).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = label.Font.Size * label.Width / stringWidth / factor;

            label.Font = new Font("Calibri", newSize);
            gr.Dispose();
        }
    }
}
